// // // pages/activity/turntable/index/index.js

let utilApp = require('../../../../utils/app');
const {
  regeneratorRuntime
} = global
Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}
// 抽奖
// 领取成功函数
function getPrizeSuccess() {
  wx.showToast({
    title: '领取成功',
  })
}


Page({
  /**
   * 页面的初始数据
   */
  data: {
    rotate: 0,
    pop: false,
    luckNum: 0,
    uid: 0,
    play_count: 0,
    isClick: true,
    isOne: 1,
    sharePop: false,
    hiddenModal: false,
    phoneNum: '',
    twoPhoneNum: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {


  },
  sharePengYouQuan() {
    utilApp.showModal('提示', '功能即将上线', '确定', function() {}, function() {}, false)
  },
  phoneNum(e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },
  twoPhoneNum(e) {
    this.setData({
      twoPhoneNum: e.detail.value
    })
  },
  listenerConfirm: async function() {
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    var myreg = /^[1][0-9][0-9]{9}$/;
    if (!myreg.test(this.data.phoneNum)) {
      utilApp.showModal('提示', '请输入正确的手机号码', '确认', function() {}, function() {}, false)
    } else if (this.data.phoneNum != this.data.twoPhoneNum) {
      utilApp.showModal('提示', '两次号码不一致', '确认', function() {}, function() {}, false)
    } else {
      let getInviter = await utilApp.lotteryDoPost('User/ajax_bind', {
        openId: userInfo.openId,
        phone: that.data.phoneNum
      });
      if (getInviter.data.status == 1) {
        that.setData({
          hiddenModal: false
        })
        utilApp.showModal('提示', '请前往公众号“猫姐帮你选”,回复数字: 55领取奖品', '复制兑换', function() {
          wx.setClipboardData({
            data: '55',
            success: function(res) {
              wx.getClipboardData({
                success: function(res) {
                  wx.showToast({
                    title: '复制成功'
                  })
                }
              })
            }
          })
        }, function() {}, false)
      } else {
        utilApp.showModal('提示', getInviter.data.msg, '确定', function() {}, function() {}, false)
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function() {
    let that = this;
    let wxGetSetting = utilApp.promisify(wx.getSetting);
    let isUserInfo = await wxGetSetting()
    let userInfo = wx.getStorageSync('userInfo');
    console.log(!userInfo)
    if (!isUserInfo.authSetting['scope.userInfo'] || !userInfo) {
      utilApp.isGetUserInfo('../../../authorization/authorization', '../activity/turntable/index/index')
      // 判断是否有储存信息  
      // if (!wx.getStorageSync('lotteryLoginMsg') || !userInfo) {
      //   let lotteryLoginMsg = wx.getStorageSync('lotteryLoginMsg');
      //   that.getUserInfo(lotteryLoginMsg.openid).then((res) => {
      //     if (res.data.userinfo.today_play_count>=3){
      //       that.setData({
      //         luckNum: parseInt(res.data.userinfo.game_num)
      //       })
      //     }else{
      //       that.setData({
      //         luckNum: parseInt(3 - res.data.userinfo.today_play_count + (res.data.userinfo.game_num - 0))
      //       })
      //     }
      //   })
      // } else {
      //   that.setLoginMsg().then((res) => {
      //     that.getUserInfo(res.openid).then((data) => {
      //       if (data.data.userinfo.today_play_count >= 3) {
      //         that.setData({
      //           luckNum: parseInt(data.data.userinfo.game_num)
      //         })
      //       } else {
      //         that.setData({
      //           luckNum: parseInt(3 - data.data.userinfo.today_play_count + (data.data.userinfo.game_num - 0))
      //         })
      //       }
      //     })
      //   })
      // }
    } else {

      let msg = await utilApp.lotteryDoPost('User/getUserInfo', {
        openId: userInfo.openId
      });
      let luckNum=0;
      if (msg.data.userinfo.is_lottery==1){
        luckNum=0
      }else{
        luckNum = 1
      }
      that.setData({
        luckNum
      })
    }
  },
  async start() {
    
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (this.data.isClick) {
      that.setData({
        isClick: false
      })

      if (that.data.luckNum <= 0) {
        that.setData({
          isClick: true
        })
        utilApp.showModal('提示', '您的抽奖次数已达到上限', '确定', function() {}, function() {}, true)
      } else {
        that.setData({
          luckNum: that.data.luckNum - 1
        })
        let lottery = await utilApp.lotteryDoPost('Lottery/lottery', {
          openId: userInfo.openId
        });
        console.log(lottery)

        if (lottery.data.status == 1) {
          let rotate = that.data.rotate;
          let num = Math.floor(Math.random() * 25 + 10);
          rotate = -1800 * that.data.isOne - (((45 * (2 - 1)) + num)) + 45
          that.setData({
            rotate: rotate
          })
          setTimeout(function() {
            that.setData({
              isClick: true,
              isOne: that.data.isOne + 1
            })
            utilApp.showModal('恭喜您', '获得5元话费+ofo骑行券+喜马拉雅券，快来领取奖品吧', '立即领取', function() {
              that.setData({
                hiddenModal: true
              })
            }, function() {}, true)
          }, 5000)
        } else {
          that.setData({
            isClick: true
          })
          utilApp.showErr(lottery.data.msg, 2000);
        } 
      } 

    }
  },
  activity() {
    wx.redirectTo({
      url: '../../../activityList/activityList',
    })
  },
  my() {
    wx.navigateTo({
      url: '../my/my',
    })
  },
  close() {
    this.setData({
      pop: false
    })
  },
  popShow() {
    this.setData({
      pop: true
    })
  },
  // 封装的函数开始
  async lottery(openId) {
    let msg = await utilApp.lotteryDoPost('Lottery/lottery', {
      openId
    });
    return msg;
  },
  async getUserInfo(openId) {
    let msg = await utilApp.lotteryDoPost('User/getUserInfo', {
      openId
    });
    return msg
  },
  // 储存登录信息 
  async setLoginMsg() {
    let json = {};
    let userInfo = wx.getStorageSync('userInfo');
    const wxLogin = utilApp.promisify(wx.login);
    let {
      code
    } = await wxLogin();

    let openId = await utilApp.lotteryDoPost('login/getSessionkey', {
      code
    });

    json.openid = openId.data.openid;
    let authLogin = await utilApp.lotteryDoPost('login/authLogin', {
      openId: openId.data.openid,
      nickName: userInfo.nickName,
      headUrl: userInfo.avatarUrl
    });
    console.log(authLogin)
    json.uid = authLogin.data.arr.uid;

    wx.setStorageSync('lotteryLoginMsg', json);
    return json;
  },
  //  封装函数结束
  onReachBottom: function() {
    sharePop: false;
  },
  popHide: function() {
    this.setData({
      sharePop: false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(ops) {
    let that = this;
    let lotteryLoginMsg = wx.getStorageSync('lotteryLoginMsg');
    let userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo.nickName)
    that.setData({
      sharePop: false
    })
    if (ops.from === 'button') {
      // 来自页面内转发按钮 
      console.log(ops.target)

    } 
    return {
      title: '西安移动幸运大抽奖',
      // path: 'pages/activity/turntable/share/share?uid=' + lotteryLoginMsg.uid + '&name=' + userInfo.nickName + '&avatarUrl=' + userInfo.avatarUrl,
      path: 'pages/activity/turntable/index/index',
      imageUrl: '../../../../image/turntable/banner.jpg',
      success: function(res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
        })
      },
      fail: function(res) {
        // 转发失败
        utilApp.showErr('分享失败', 2000)
      }
    }
  }
})